//
//  RecordSlide.m
//  MUBATE
//
//  Created by sunbin on 2017/2/23.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "RecordSlide.h"
#import "FFPagingViewController.h"
#import "Record.h"

#define LRRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;
#define kHeaderViewTop      40


@interface RecordSlide ()<FFPagingViewControllerDataSource, FFPagingViewControllerDelegate>
@property(nonatomic, strong) FFPagingViewController *pagingViewController;
@property(nonatomic, strong) FFPagingHeaderView *pagingHeaderView;

@property(nonatomic, strong) NSMutableArray *viewControllers;


@end

@implementation RecordSlide

#pragma mark - init
- (void)setupView
{
    self.title = @"记录";
    
    [self.view addSubview:self.pagingHeaderView];
    WS(weakSelf);
    // pagingViewItemClickHandle被点击
    self.pagingHeaderView.pagingViewItemClickHandle = ^(FFPagingHeaderView *headerView, NSString *title, NSInteger currentIndex) {
        weakSelf.pagingViewController.seletedIndex = currentIndex;
        Record *record = [weakSelf.viewControllers objectAtIndex:currentIndex];
        record.currentIndex = currentIndex;
        
    };
    
    [self.view addSubview:self.pagingViewController.view];
    [self.pagingViewController reloadData];
}

- (void)setupData
{
    //创建controller
    for (NSInteger i=0; i<self.pagingHeaderView.titles.count; i++) {
        Record *vc = [[Record alloc] initWithNibName:@"Record" bundle:nil];
        vc.view.backgroundColor = LRRandomColor;
        [self.viewControllers addObject:vc];
    }
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
    }

    [self setupData];
    [self setupView];
    
    // 查看交易记录详情 实现跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRecordDetail:) name:CHECKRECORDDETAILS object:nil];
}
- (void)checkRecordDetail:(NSNotification *)noti{
    NSDictionary *dic = noti.userInfo;

    [self.navigationController pushViewController:[dic objectForKey:@"key"] animated:NO];
}
- (void)viewDidLayoutSubviews
{
    self.pagingViewController.view.frame = CGRectMake(0, kHeaderViewTop, self.view.frame.size.width, self.view.frame.size.height- kHeaderViewTop);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - FFPagingViewControllerDelegate
// 滑动监听
- (void)customPagingViewController:(FFPagingViewController *)pagingViewController slideIndex:(NSInteger)slideIndex
{
    Record *record = [self.viewControllers objectAtIndex:slideIndex];
    record.currentIndex = slideIndex;
    

    [self.pagingHeaderView updateTitleContentOffset:slideIndex];
}

- (void)customPagingViewController:(FFPagingViewController *)pagingViewController contentOffset:(CGPoint)slideOffset
{
    self.pagingHeaderView.contentOffset = slideOffset;
}

#pragma mark - FFPagingViewControllerDataSource
- (NSUInteger)numberOfChildViewControllersInPagingViewController:(FFPagingViewController *)pagingViewController
{
    return self.viewControllers.count;
}

- (UIViewController *)pagingViewController:(FFPagingViewController *)pageingViewController atIndex:(NSInteger)index
{
    return self.viewControllers[index];
}

#pragma mark - getter
- (FFPagingViewController *)pagingViewController
{
    if (!_pagingViewController) {
        _pagingViewController = [[FFPagingViewController alloc] init];
        _pagingViewController.delegate = self;
        _pagingViewController.dataSource = self;
    }
    return _pagingViewController;
}

- (FFPagingHeaderView *)pagingHeaderView
{
    if (!_pagingHeaderView) {
        _pagingHeaderView = [[FFPagingHeaderView alloc] init];
        _pagingHeaderView.backgroundColor = [UIColor whiteColor];
        _pagingHeaderView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kHeaderViewTop);
        _pagingHeaderView.itemWidth = 80;
        _pagingHeaderView.titles = @[@"全部", @"交易", @"返现"];
    }
    return _pagingHeaderView;
}

- (NSMutableArray *)viewControllers
{
    if (!_viewControllers) {
        _viewControllers = [NSMutableArray array];
    }
    return _viewControllers;
}


@end
